home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / contrib / bpickle.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  5.0 KB  |  168 lines

  1. """
  2. Copyright (c) 2006, Gustavo Niemeyer <gustavo@niemeyer.net>
  3.  
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8.  
  9.     * Redistributions of source code must retain the above copyright notice,
  10.       this list of conditions and the following disclaimer.
  11.     * Redistributions in binary form must reproduce the above copyright notice,
  12.       this list of conditions and the following disclaimer in the documentation
  13.       and/or other materials provided with the distribution.
  14.     * Neither the name of the copyright holder nor the names of its
  15.       contributors may be used to endorse or promote products derived from
  16.       this software without specific prior written permission.
  17.  
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """
  30.  
  31.  
  32. dumps_table = {}
  33. loads_table = {}
  34.  
  35.  
  36. def dumps(obj, _dt=None):
  37.     if not _dt:
  38.         _dt = dumps_table
  39.  
  40.     type_names = [type(obj)]
  41.     for type_name in type_names:
  42.         if _dt.has_key(type_name):
  43.             return _dt[type_name](obj)
  44.  
  45.         type_names.extend(type_name.__bases__)
  46.  
  47.     raise ValueError, "Unsupported type: %s" % type(obj)
  48.  
  49.  
  50. def loads(str, _lt=loads_table):
  51.     if not str:
  52.         raise ValueError, "Can't load empty string"
  53.     try:
  54.         return _lt[str[0]](str, 0)[0]
  55.     except KeyError, e:
  56.         raise ValueError, "Unknown type character: %s" % e
  57.     except IndexError:
  58.         raise ValueError, "Corrupted data"
  59.  
  60. def dumps_bool(obj):
  61.     return "b%d" % int(obj)
  62.  
  63. def dumps_int(obj):
  64.     return "i%s;" % obj
  65.  
  66. def dumps_float(obj):
  67.     return "f%r;" % obj
  68.  
  69. def dumps_str(obj):
  70.     return "s%s:%s" % (len(obj), obj)
  71.  
  72. def dumps_unicode(obj):
  73.     obj = obj.encode("utf-8")
  74.     return "u%s:%s" % (len(obj), obj)
  75.  
  76. def dumps_list(obj, _dt=None):
  77.     return "l%s;" % "".join([dumps(val, _dt) for val in obj])
  78.  
  79. def dumps_tuple(obj, _dt=None):
  80.     return "t%s;" % "".join([dumps(val, _dt) for val in obj])
  81.  
  82. def dumps_dict(obj, _dt=None):
  83.     res = []
  84.     keys = sorted(obj.keys())
  85.     append = res.append
  86.     for key in keys:
  87.         val = obj[key]
  88.         append(dumps(key, _dt))
  89.         append(dumps(val, _dt))
  90.     return "d%s;" % "".join(res)
  91.  
  92. def dumps_none(obj):
  93.     return "n"
  94.  
  95. def loads_bool(str, pos):
  96.     return bool(int(str[pos+1])), pos+2
  97.  
  98. def loads_int(str, pos):
  99.     endpos = str.index(";", pos)
  100.     return int(str[pos+1:endpos]), endpos+1
  101.  
  102. def loads_float(str, pos):
  103.     endpos = str.index(";", pos)
  104.     return float(str[pos+1:endpos]), endpos+1
  105.  
  106. def loads_str(str, pos):
  107.     startpos = str.index(":", pos)+1
  108.     endpos = startpos+int(str[pos+1:startpos-1])
  109.     return str[startpos:endpos], endpos
  110.  
  111. def loads_unicode(str, pos):
  112.     startpos = str.index(":", pos)+1
  113.     endpos = startpos+int(str[pos+1:startpos-1])
  114.     return str[startpos:endpos].decode("utf-8"), endpos
  115.  
  116. def loads_list(str, pos, _lt=loads_table):
  117.     pos += 1
  118.     res = []
  119.     append = res.append
  120.     while str[pos] != ";":
  121.         obj, pos = _lt[str[pos]](str, pos)
  122.         append(obj)
  123.     return res, pos+1
  124.  
  125. def loads_tuple(str, pos, _lt=loads_table):
  126.     pos += 1
  127.     res = []
  128.     append = res.append
  129.     while str[pos] != ";":
  130.         obj, pos = _lt[str[pos]](str, pos)
  131.         append(obj)
  132.     return tuple(res), pos+1
  133.  
  134. def loads_dict(str, pos, _lt=loads_table):
  135.     pos += 1
  136.     res = {}
  137.     while str[pos] != ";":
  138.         key, pos = _lt[str[pos]](str, pos)
  139.         val, pos = _lt[str[pos]](str, pos)
  140.         res[key] = val
  141.     return res, pos+1
  142.  
  143. def loads_none(str, pos):
  144.     return None, pos+1
  145.  
  146.  
  147. dumps_table.update({       bool: dumps_bool,
  148.                             int: dumps_int,
  149.                            long: dumps_int,
  150.                           float: dumps_float,
  151.                             str: dumps_str,
  152.                         unicode: dumps_unicode,
  153.                            list: dumps_list,
  154.                           tuple: dumps_tuple,
  155.                            dict: dumps_dict,
  156.                      type(None): dumps_none     })
  157.  
  158. loads_table.update({ "b": loads_bool,
  159.                      "i": loads_int,
  160.                      "f": loads_float,
  161.                      "s": loads_str,
  162.                      "u": loads_unicode,
  163.                      "l": loads_list,
  164.                      "t": loads_tuple,
  165.                      "d": loads_dict,
  166.                      "n": loads_none     })
  167.  
  168.